<?php
/**
 * RedXenon Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE.txt
 * A copy of this license can be found in the world-wide-web at this URL:
 * http://www.redxenon.com/license/new-bsd-license.txt * 
 */

/* Abstract class */
require_once("RXAbstractFormElement.php");

/**
 * RedXenon Form (RXDateSelect.php)
 * Defines methods to create Input elements
 * 
 * @category RedXenon
 * @package RedXenon_Form
 * @copyright Copyright (c) 2011 Red Xenon (http://www.redxenon.com)
 * @version $Id: RXDateSelect.php 1.0 2011-05-02 $ 
 */
class RXDateSelect extends RXAbstractFormElement{
	
	/**
	 * Date value in UNIX Epoch
	 * @var Integer
	 */
	private $value;
	
	/**
	 * Order of the select fields
	 * @var String
	 */
	private $format;
	
	/**
	 * Constructor 
	 * @param String $name
	 * @param String $value
	 * @param String $format
	 */
	public function __construct($name = NULL, $value = NULL, $format = "YMD"){
		$this->value = $value;
		$this->format = $format;
		parent::__construct($name);
	}
	
	/**
	 * Sets the time value for the date field
	 * The date must be in Unix Epoch Format
	 * @param Integer $value
	 */
	public function setValue($value){
		$this->value = $value;
	}	
	
	/**
	 * Sets the order format of the select fields
	 * and which ones to show
	 * Y = Year Select
	 * M = Month Select
	 * D = Day Select
	 * H = Hour Select
	 * I = Minutes Select
	 * @param String $format
	 */
	public function setFormat($format){
		$this->format = strtoupper($format);
	}
	
	/**
	 * Returns a HTML code that represents a 
	 * Select field for the Day value
	 * @param Integer $daySelected
	 */
	public function getDaySelect($daySelected = NULL){
		$ret = '<select name="'.$this->name.'-day">\n';
		for($day = 1; $day <= 31; $day ++){
			if($daySelected == $day){
				$ret .= '\t<option value="'.$day.'" selected>'.$day.'</option>\n';
			}else{
				$ret .= '\t<option value="'.$day.'">'.$day.'</option>\n';
			}
		}
		$ret .= '</select>\n';
		return $ret;
	}
	
	/**
	 * Returns a HTML code that represents a 
	 * Select field for the Month value
	 * @param Integer $monthSelected
	 */
	public function getMonthSelect($monthSelected = NULL){
		$ret = '<select name="'.$this->name.'-month">\n';
		for($month = 1; $month <= 12; $month ++){
			if($monthSelected == $month){
				$ret .= '\t<option value="'.$month.'" selected>'.$month.'</option>\n';
			}else{
				$ret .= '\t<option value="'.$month.'">'.$month.'</option>\n';
			}
		}
		$ret .= '</select>\n';
		return $ret;
	}
	
	/**
	 * Returns a HTML code that represents a 
	 * Select field for the Year value
	 * @param Integer $yearSelected
	 * @param Integer $startingYear
	 */
	public function getYearSelect($yearSelected = NULL, $startingYear = 1950){
		$ret = '<select name="'.$this->name.'-year">\n';
		for($year = $startingYear; $year <= (date("Y")+1); $year ++){
			if($yearSelected == $year){
				$ret .= '\t<option value="'.$year.'" selected>'.$year.'</option>\n';
			}else{
				$ret .= '\t<option value="'.$year.'">'.$year.'</option>\n';
			}
		}
		$ret .= '</select>\n';
		return $ret;
	}

	/**
	 * Returns a HTML code that represents a 
	 * Select field for the Hour value
	 * @param Integer $hourSelected
	 */
	public function getHourSelect($hourSelected = NULL){
		$ret = '<select name="'.$this->name.'-hour">\n';
		for($hour = 0; $hour <= 23; $hour ++){
			if($hourSelected == $hour){
				$ret .= '\t<option value="'.$hour.'" selected>'.$hour.'</option>\n';
			}else{
				$ret .= '\t<option value="'.$hour.'">'.$hour.'</option>\n';
			}
		}
		$ret .= '</select>\n';
		return $ret;
	}
	
	/**
	 * Returns a HTML code that represents a 
	 * Select field for the Minute value
	 * @param Integer $minuteSelected
	 * @return String
	 */
	public function getMinuteSelect($minuteSelect = NULL){
		$ret = '<select name="'.$this->name.'-minute">\n';
		for($minute = 0; $minute <= 59; $minute ++){
			if($minuteSelect == $minute){
				$ret .= '\t<option value="'.$minute.'" selected>'.$minute.'</option>\n';
			}else{
				$ret .= '\t<option value="'.$minute.'">'.$minute.'</option>\n';
			}
		}
		$ret .= '</select>\n';
		return $ret;
	}
	
	/**
	 * Returns a HTML of the whole select field
	 * @return String
	 */
	public function getOutput(){
		$ret = "";
		$selectedDay = NULL;
		$selectedMonth = NULL;
		$selectedYear = NULL;
		$selectedHour = NULL;
		$selectedMinute = NULL;
		
		if($this->value){
			$selectedDay = date("j", $this->value);
			$selectedMonth = date("n", $this->value);
			$selectedYear = date("Y", $this->value);
			$selectedHour = date("H", $this->value);
			$selectedMinute = date("i", $this->value);
		}
		
		foreach(str_split($this->format) as $char){
			switch(strtoupper($char)){
				case "Y": $ret .= $this->getYearSelect($selectedYear); break;
				case "M": $ret .= $this->getMonthSelect($selectedMonth); break;
				case "D": $ret .= $this->getDaySelect($selectedDay); break;
				case "H": $ret .= $this->getHourSelect($selectedHour); break;
				case "I": $ret .= $this->getMinuteSelect($selectedMinute); break;
			}			
		}

		return $ret;
	}
}

?>